Unit BSQLUnit;

{$mode objfpc}{$H+}

Interface

Uses
  Classes, SysUtils, BSQLBuilderUnit;

Type

{ BSQLParamClass }

BSQLParamClass = Class
  Private
    bIterator: String;
    bStream: TMemoryStream;
    bValue: String;
    bWithStream: Boolean;
  Public
    Property WithStream: Boolean Read bWithStream;
    Property Value: String Read bValue;
    Property Iterator: String Read bIterator;
    Property Stream: TMemoryStream Read bStream;
    Constructor Build(Const aIterator, aValue: String);
    Constructor Build(Const aIterator: String; Const aStream: TMemoryStream);
    Destructor Burn;
End;

Type

{ BSQLClass }

BSQLClass = Class
  Private
    bIsProcedure: Boolean;
    bParams: TList;
    bSQL: String;
    bSQLBuilder: BSQLBuilderClass;
    Procedure ClearParam(Const aIterator: String);
    Procedure PurgeParams;
    Function GetSQL: String;
  Public
    Property SQL: String Read GetSQL;
    Property Params: TList Read bParams;
    Property IsProcedure: Boolean Read bIsProcedure Write bIsProcedure;
    Property Builder: BSQLBuilderClass Read bSQLBuilder;

    Procedure AddParam(Const aIterator, aValue: String);
    Procedure AddParam(Const aIterator: String; Const aValue: Boolean);
    Procedure AddParam(Const aIterator: String; Const aValue: Integer);
    Procedure AddParam(Const aIterator: String; Const aValue: Double);
    Procedure AddParam(Const aIterator: String; Const aValue: TDateTime;
      Const aFormat: String);
    Procedure AddParam(Const aIterator: String; Const aStream: TMemoryStream);

    Procedure Clear;

    Constructor Build(Const aString: String;
      Const ForProcedure: Boolean = FALSE);
    Constructor Build(Const aMask: String; Const aParams: Array Of Const;
      Const ForProcedure: Boolean = FALSE);
    Constructor Build;
    Destructor Burn;
End;

Implementation

{ BSQLParamClass }

constructor BSQLParamClass.Build(const aIterator, aValue: String);
begin
  Inherited Create;
  bIterator := aIterator;
  bValue := aValue;
  bStream := nil;
end;

Constructor BSQLParamClass.Build(Const aIterator: String;
  Const aStream: TMemoryStream);
Begin
  Inherited Create;
  bIterator := aIterator;
  bStream := TMemoryStream.Create;
  Stream.LoadFromStream(aStream);
End;

Destructor BSQLParamClass.Burn;
Begin
  If Not(Stream = nil) Then
    Begin
      Stream.Clear;
      Stream.Free;
    End;
  Inherited Destroy;
End;

{ BSQLClass }

Procedure BSQLClass.AddParam(Const aIterator: String; Const aValue: Integer);
Begin
  ClearParam(aIterator);
  Params.Add(BSQLParamClass.Build(aIterator, IntToStr(aValue)));
End;

Procedure BSQLClass.AddParam(Const aIterator: String; Const aValue: Double);
Begin
  ClearParam(aIterator);
  Params.Add(BSQLParamClass.Build(aIterator, FloatToStr(aValue)));
End;

Procedure BSQLClass.AddParam(Const aIterator: String; Const aValue: TDateTime;
  Const aFormat: String);
Begin
  ClearParam(aIterator);
  Params.Add(BSQLParamClass.Build(aIterator, FormatDateTime(aFormat, aValue)));
End;

Procedure BSQLClass.AddParam(Const aIterator: String;
  Const aStream: TMemoryStream);
Begin
  ClearParam(aIterator);
  Params.Add(BSQLParamClass.Build(aIterator, aStream));
End;

Procedure BSQLClass.Clear;
Begin
  Builder.Clear;
  PurgeParams;
End;

Procedure BSQLClass.ClearParam(Const aIterator: String);
Var
  i: Integer;
Begin
  For i := 0 To Params.Count - 1 Do
    If BSQLParamClass(Params[i]).Iterator = aIterator Then
      Begin
        BSQLParamClass(Params[i]).Burn;
        Params.Delete(i);
        Exit;
      End;
End;

Procedure BSQLClass.PurgeParams;
Var
  i: Integer;
Begin
  For i := 0 To Params.Count - 1 Do
    BSQLParamClass(Params[i]).Burn;
  Params.Clear;
End;

Function BSQLClass.GetSQL: String;
Begin
  If bSQLBuilder = nil Then Result := bSQL
  Else
    Begin
      Result := Builder.Text;
      IsProcedure := Builder.ExecutableQuery;
    End;
end;

procedure BSQLClass.AddParam(const aIterator, aValue: String);
begin
  ClearParam(aIterator);
  Params.Add(BSQLParamClass.Build(aIterator, aValue));
end;

Procedure BSQLClass.AddParam(Const aIterator: String; Const aValue: Boolean);
Begin
  ClearParam(aIterator);
  Params.Add(BSQLParamClass.Build(aIterator, IntToStr(Integer(aValue))));
End;

Constructor BSQLClass.Build(Const aString: String; Const ForProcedure: Boolean);
Begin
  Inherited Create;
  bParams := TList.Create;
  bSQL := aString;
  bIsProcedure := ForProcedure;
End;

Constructor BSQLClass.Build(Const aMask: String; Const aParams: Array Of Const;
  Const ForProcedure: Boolean);
Begin
  Build(Format(aMask, aParams), ForProcedure);
End;

Constructor BSQLClass.Build;
Begin
  bSQLBuilder := BSQLBuilderClass.Build;
  bParams := TList.Create;
End;

Destructor BSQLClass.Burn;
Begin
  PurgeParams;
  Params.Free;

  If Not(bSQLBuilder = nil) Then bSQLBuilder.Burn;
End;

End.

